package messagePacket;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import Util.Util;
import io.netty.channel.Channel;
import server.ChattingServeHandler;

public class AddFriendPacket extends Packet {
	String sendUser;
	String receiveUser;
	String message;
	// 3 represent request add friend 4 represent agree friend request 5 represent disagree friend request
	int messageType;  
	

	public AddFriendPacket(String sendUser, String receiveUser, String message, int messageType) {
		super();
		this.sendUser = sendUser;
		this.receiveUser = receiveUser;
		this.message = message;
		this.messageType = messageType;
	}

	public AddFriendPacket() {
	}
 
	@Override
	public void decode(byte[] buffer) throws UnsupportedEncodingException {  
		int start_pos = 4;
    	// ��ȡ����
    	int length = Util.bytes2int(buffer, start_pos);
    	start_pos += 4;
    	
        this.sendUser = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;


        length = Util.bytes2int(buffer, start_pos);

    	start_pos += 4;
        this.receiveUser = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;

     
        length = Util.bytes2int(buffer, start_pos);

    	start_pos += 4;
        this.message = new String(buffer, start_pos, length, "UTF-8");
        
        start_pos += length;
        this.messageType = Util.bytes2int(buffer, start_pos);
	}

	@Override
	public byte[] encode() throws UnsupportedEncodingException {
		int sendUserLength = this.sendUser.getBytes("UTF-8").length;
        int receiveUserLength = this.receiveUser.getBytes("UTF-8").length;
        int messageLength = this.message.getBytes("UTF-8").length;
        int totalLength = 4 + 4 + sendUserLength + 4 + receiveUserLength + 4 + messageLength + 4;
        byte[] buffer = new byte[totalLength];
        int offset = 0;

        // add packet type to buffer
        System.arraycopy(Util.int2bytes(Util.PACKET_ADD_FRIEND), 0, buffer, offset, 4);
        offset += 4;
        
        // the four bytes store send user name length
        System.arraycopy(Util.int2bytes(sendUserLength), 0, buffer, offset, 4);
        offset += 4;
        
        // copy send user to buffer
        System.arraycopy(sendUser.getBytes("UTF-8"), 0, buffer, offset, sendUserLength);
        offset += sendUserLength;
        
        // the four bytes store receive user name length
        System.arraycopy(Util.int2bytes(receiveUserLength), 0, buffer, offset, 4);
        offset += 4;
        
        //copy receive user to buffer
        System.arraycopy(receiveUser.getBytes("UTF-8"), 0, buffer, offset, receiveUserLength );
        offset += receiveUserLength;

        System.arraycopy(Util.int2bytes(messageLength), 0, buffer, offset, 4);
        offset += 4;
        
        // copy message to buffer
        System.arraycopy(message.getBytes("UTF-8"), 0, buffer, offset, messageLength);
        offset += messageLength;
        
        // copy message type  to buffer
        System.arraycopy(Util.int2bytes(this.messageType), 0, buffer, offset, 4);
        offset += 4;

        return buffer;
	}
	

	public String getSendUser() {
		return sendUser;
	}

	public void setSendUser(String sendUser) {
		this.sendUser = sendUser;
	}

	public String getReceiveUser() {
		return receiveUser;
	}

	public void setReceiveUser(String receiveUser) {
		this.receiveUser = receiveUser;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getMessageType() {
		return messageType;
	}

	public void setMessageType(int messageType) {
		this.messageType = messageType;
	}

	@Override
	public void process(Channel channel, Packet packet) {
		AddFriendPacket addFriendPacket = (AddFriendPacket) packet;
		System.out.println("addFriendPacket messageType" + addFriendPacket.getMessageType());
		switch (addFriendPacket.getMessageType()) {
			case 3: // add friend request
				if(ChattingServeHandler.uc.getChannel(addFriendPacket.receiveUser) == null) {
					 MessagePacket cmwarning=new MessagePacket("服务器", addFriendPacket.sendUser, "该用户不在线" ,2);
				     channel.writeAndFlush(cmwarning);
				     ChattingServeHandler.msgQueue.addMsg(addFriendPacket.getReceiveUser(), addFriendPacket);
				}else {
					ChattingServeHandler.uc.getChannel(addFriendPacket.getReceiveUser()).writeAndFlush(addFriendPacket);
				}
				break;
			case 4: // agree friend request
				// if the receive user's friend list is not null 
				if(ChattingServeHandler.friends.get(addFriendPacket.getReceiveUser()) != null) {
					// get original HashMap, then add the new user in the map
	        		HashMap<Integer, String> hashMap = ChattingServeHandler.friends.get(addFriendPacket.getReceiveUser());
	        		hashMap.put(hashMap.size()+1, addFriendPacket.getSendUser());
	        		// replace the old map with the new map
	        		ChattingServeHandler.friends.put(addFriendPacket.getReceiveUser(), hashMap);
	        		// if the send user's friend list is not null
	        		updateSendUserFriendList(addFriendPacket);
	        	}else { // if the receive user's friend list is null
	        		HashMap<Integer, String> map = new HashMap<Integer, String>();
	        		map.put(0, addFriendPacket.getSendUser());
	            	ChattingServeHandler.friends.put(addFriendPacket.getReceiveUser(), map);
	            	updateSendUserFriendList(addFriendPacket);
	        	}
	        	addFriend(channel, addFriendPacket);
				break;
			case 5: // disagree friend request
				addFriend(channel, addFriendPacket);
				break;
			default:
				break;
		}
		
	}

	private void updateSendUserFriendList(AddFriendPacket addFriendPacket) {
		if(ChattingServeHandler.friends.get(addFriendPacket.getSendUser()) != null) {
			// get original map
			HashMap<Integer, String> map1 = ChattingServeHandler.friends.get(addFriendPacket.getSendUser());
			// put the new friend
			map1.put(map1.size()+1, addFriendPacket.getReceiveUser());
			// replace the old map with a new one
			ChattingServeHandler.friends.put(addFriendPacket.getSendUser(), map1);
		}else { // if the send user's friend list is null
			HashMap<Integer, String> map = new HashMap<Integer, String>();
			map.put(0, addFriendPacket.getReceiveUser());
			ChattingServeHandler.friends.put(addFriendPacket.getSendUser(), map);
		}
	}

	private void addFriend(Channel newchannel, AddFriendPacket cmsg) {
		if(ChattingServeHandler.uc.getChannel(cmsg.getReceiveUser())==null){
		     MessagePacket cmwarning = new MessagePacket("服务器", cmsg.getSendUser(),"该用户不在线" ,2);
		     newchannel.writeAndFlush(cmwarning);
		     ChattingServeHandler.msgQueue.addMsg(cmsg.getReceiveUser(), cmsg);
		 }else{
			 ChattingServeHandler.uc.getChannel(cmsg.getReceiveUser()).writeAndFlush(cmsg);
		 }
	}
	
}
